package com.power.using.mymvptoutiao.module.base;

import android.os.Bundle;
import android.support.annotation.Nullable;


/**
 * Created by 聪明一只哈 on 2019/9/28.
 * Time: 2019/9/28  9:05
 */
public abstract class LazyLoadFragment<T extends IBasePresenter> extends BaseFragment<T>{


    /**
     * 是否初始化过布局
     */
    protected boolean isViewInitiated;
    /**
     * 当前界面是否可见
     */
    protected boolean isVisibleToUser;
    /**
     * 是否加载过数据
     */
    protected boolean isDataInitiated;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated=true;
        prefareFetchData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser=isVisibleToUser;
        if(isVisibleToUser){
            prefareFetchData();
        }
    }

    protected void prefareFetchData(){
        prepareFetchData(false);
    }

    /**
     * 判断懒加载条件
     *
     * @param forceUpdate 强制更新，好像没什么用？
     */
    public void prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            fetchData();
            isDataInitiated = true;
        }
    }

    /**
     * 懒加载
     */
    protected abstract void fetchData();
}
